using System.Collections.Generic;
using Godot;

namespace GoGameClient.script.node.caption;

public partial class AnimationControl : Node2D
{
    private int _speed = 1000;
    private int _manSpeed = 1000;
    private ParallaxBackground _mask;
    private ParallaxBackground _prospect;
    private ParallaxBackground _background;

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        _mask = GetNode<ParallaxBackground>("Mask");
        _prospect = GetNode<ParallaxBackground>("Prospect");
        _background = GetNode<ParallaxBackground>("Background");
        var hour = (int)Time.GetDatetimeDictFromSystem().GetValueOrDefault("hour");
        switch (hour)
        {
            case > 0 and (< 6 or > 20):
                LoadResources("night");
                break;
            case >= 17 and <= 20:
                LoadResources("evening");
                break;
            default:
                LoadResources("morning");
                break;
        }
    }


    private void LoadResources(string res)
    {
        var texture = (Texture2D)GD.Load($"res://resources/Ui/notice/notice-background-{res}.png");
        GetNode<Sprite2D>("Background/ParallaxLayer/BackImage").Texture = texture;
        texture = (Texture2D)GD.Load($"res://resources/Ui/notice/notice-prospect-{res}.png");
        var image = GetNode<Sprite2D>("Prospect/ParallaxLayer/ProspectImage");
        image.Translate(new Vector2(0, GetWindow().Size.Y - texture.GetHeight()));
        image.Texture = texture;
        var parallaxLayer = GetNode<ParallaxLayer>("Prospect/ParallaxLayer");
        parallaxLayer.MotionMirroring = new Vector2(texture.GetWidth(), 0);
        if (res == "morning")
        {
            GetNode<ParallaxBackground>("Mask").Hide();
            return;
        }

        texture = (Texture2D)GD.Load($"res://resources/Ui/notice/notice-mask-{res}.png");
        image = GetNode<Sprite2D>("Mask/ParallaxLayer/MaskImage");
        image.Texture = texture;
        image.Translate(new Vector2(0, GetWindow().Size.Y - texture.GetHeight()));
        parallaxLayer = GetNode<ParallaxLayer>("Mask/ParallaxLayer");
        parallaxLayer.MotionMirroring = new Vector2(texture.GetWidth(), 0);
    }

    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
        _mask.ScrollOffset += new Vector2((float)(delta * _manSpeed / 20),0);
        _prospect.ScrollOffset += new Vector2((float)(delta * _manSpeed / 50),0);
        _background.ScrollOffset += new Vector2((float)(delta * _manSpeed / 100),0);
    }
}